/*
 * GameScreen.cpp
 *
 *  Created on: 27 mars 2013
 *      Author: Aurel
 */

#include "display/GameScreen.hpp"

GameScreen::GameScreen(ThreadDisplay * p_screenManager) : 	Displayable(p_screenManager),
m_labelPlayerName(), 	m_labelIP(), 	m_PlayerName(), 	m_IP(),
m_labelEnter(),			m_cursor(),		m_FocusRow(0)
{
	m_labelPlayerName	.SetPosition(GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_BORDER_LEFT, GraphicValue::TABLE_SCORE_BORDER);
	m_labelIP			.SetPosition(GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_BORDER_LEFT, GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_HEIGHT_ROW);
	m_PlayerName		.SetPosition(GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_WIDTH_COLUMN_COUNTER * 3, GraphicValue::TABLE_SCORE_BORDER);
	m_IP				.SetPosition(GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_WIDTH_COLUMN_COUNTER * 3, GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_HEIGHT_ROW);
	m_labelEnter		.SetPosition(GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_WIDTH_COLUMN_COUNTER * 3, GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_HEIGHT_ROW * 2);
	m_cursor			.SetPosition(0, GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_HEIGHT_ROW * m_FocusRow);

	m_labelPlayerName	.SetSize(GraphicValue::TABLE_SCORE_POLICE_SIZE);
	m_labelIP			.SetSize(GraphicValue::TABLE_SCORE_POLICE_SIZE);
	m_PlayerName		.SetSize(GraphicValue::TABLE_SCORE_POLICE_SIZE);
	m_IP				.SetSize(GraphicValue::TABLE_SCORE_POLICE_SIZE);
	m_labelEnter		.SetSize(GraphicValue::TABLE_SCORE_POLICE_SIZE);
	m_cursor			.SetSize(GraphicValue::TABLE_SCORE_POLICE_SIZE);

	m_labelPlayerName	.SetColor(sf::Color::White);
	m_labelIP			.SetColor(sf::Color::White);
	m_PlayerName		.SetColor(sf::Color::White);
	m_IP				.SetColor(sf::Color::White);
	m_labelEnter		.SetColor(sf::Color::White);
	m_cursor			.SetColor(sf::Color::White);

	m_labelPlayerName	.SetText("Player Name:");
	m_labelIP			.SetText("IP address:");
	m_labelEnter		.SetText("Press enter to connect !");
	m_cursor			.SetText("=>");
	m_IP				.SetText(GameInfo::m_IP);
	m_PlayerName		.SetText(GameInfo::m_PlayerName);
}

GameScreen::~GameScreen() {
}

/**
 * Draw all ellement inside the window
 */
void GameScreen::draw(sf::RenderWindow* p_window)
{
	p_window->Draw(m_labelPlayerName);
	p_window->Draw(m_labelIP);
	p_window->Draw(m_PlayerName);
	p_window->Draw(m_IP);
	p_window->Draw(m_labelEnter);
	p_window->Draw(m_cursor);
}

/**
 * Catch keyboard event to know which module launch
 */
void GameScreen::DoEvent(sf::Event e)
{
	if ((e.Type == sf::Event::KeyPressed))
	{
		if(e.Key.Code == sf::Key::Return)
		{
			this->LaunchGame();
		}
		else if(e.Key.Code == sf::Key::Escape)
		{
			this->BackToMainScreen();
		}
		else if(e.Key.Code == sf::Key::Back)
		{
			this->RemoveCharacterOnSelectedRow();
		}
		else if(e.Key.Code == sf::Key::Tab)
		{
			//change selected Row
			if(++m_FocusRow > 1)
				m_FocusRow = 0;
			m_cursor.SetY(GraphicValue::TABLE_SCORE_BORDER + GraphicValue::TABLE_SCORE_HEIGHT_ROW * m_FocusRow);
		}
	}
	else if(e.Type == sf::Event::TextEntered && e.Text.Unicode >= 32)
	{
		this->AddCharacterOnSelectedRow(static_cast<char>(e.Text.Unicode));
	}
}
/**
 * Not implemented
 */
void GameScreen::DoRealTimeEvent(const sf::Input& p_event){

}
/**
 * Add a character To the selected Row defined by m_focusRow
 */
void GameScreen::RemoveCharacterOnSelectedRow()
{
	if(m_FocusRow == 0)
	{
		if(GameInfo::m_PlayerName.length() > 0)
			GameInfo::m_PlayerName = GameInfo::m_PlayerName.substr(0, GameInfo::m_PlayerName.length()-1);
		m_PlayerName.SetText(GameInfo::m_PlayerName);

	}
	else
	{
		if(GameInfo::m_IP.length() > 0)
			GameInfo::m_IP = GameInfo::m_IP.substr(0, GameInfo::m_IP.length()-1);
		m_IP.SetText(GameInfo::m_IP);
	}
}
/**
 * Remove a character To the selected Row defined by m_focusRow
 */
void GameScreen::AddCharacterOnSelectedRow(char p_Char)
{
	if(m_FocusRow == 0)
	{
		GameInfo::m_PlayerName += p_Char;

		m_PlayerName.SetText(GameInfo::m_PlayerName);

	}
	else
	{
		GameInfo::m_IP += p_Char;

		m_IP.SetText(GameInfo::m_IP);
	}
}
/**
 * Back
 */
void GameScreen::BackToMainScreen()
{
	Debug::printTrace(Debug::information,"display/ServerScreen::LaunchGame", "Launch the server");

	MainScreen* menu = new MainScreen(this->GetScreenManager());

	this->GetScreenManager()->setDisplayable(menu);
}
/**
 * Start game
 */
void GameScreen::LaunchGame()
{
	Debug::printTrace(Debug::information,"display/ServerScreen::LaunchGame", "Launch the game");

	GameGraphic* game = new GameGraphic(this->GetScreenManager());

	if(game->GetNetworkManager()->IsRunning())
		this->GetScreenManager()->setDisplayable(game);
}
